#!/bin/bash

do_install_nerdctl()
{
    # Check if nerdctl exists
    if ! command -v nerdctl &> /dev/null
    then
        echo "nerdctl command not found, starting download..."

        # Set architecture variable
        arch=""
        case "$(uname -m)" in
            arm64|aarch64)
                arch="arm64"
                ;;
            x86_64|amd64)
                arch="amd64"
                ;;
            *)
                echo "Unsupported architecture: $(uname -m)"
                exit 1
                ;;
        esac

        # Set nerdctl version
        nerdctl_version="2.0.2"

        # Download and extract nerdctl in a single command using ghproxy.net
        echo "Downloading and extracting nerdctl v${nerdctl_version} (linux-${arch})..."
        curl -sSL "https://ghproxy.net/https://github.com/containerd/nerdctl/releases/download/v${nerdctl_version}/nerdctl-${nerdctl_version}-linux-${arch}.tar.gz" | tar -xz -C /tmp/ nerdctl
	chmod u+x /tmp/nerdctl
        # Move to /usr/local/bin (or /usr/bin if preferred)
        echo "Installing nerdctl to /usr/local/bin/..."
        sudo mv /tmp/nerdctl /usr/local/bin/nerdctl

        echo "nerdctl v${nerdctl_version} has been successfully installed."
    else
        echo "nerdctl already exists."
    fi
}


do_install_docker_wrapper()
{
    # Check if nerdctl exists
    if command -v nerdctl &> /dev/null
    then
        # Check if docker wrapper already exists
        if [ -f /usr/bin/docker ] && [ "$(head -n 1 /usr/bin/docker)" = "#!/bin/bash" ]; then
            echo "docker wrapper already exists."
        else
            echo "nerdctl command found, creating docker wrapper..."

            # Create docker wrapper script
            echo "#!/bin/bash" | sudo tee /usr/bin/docker > /dev/null
            echo "nerdctl --namespace k8s.io \$@" | sudo tee -a /usr/bin/docker > /dev/null

            # Add execute permission
            sudo chmod +x /usr/bin/docker

            echo "docker wrapper has been successfully created."
        fi
    else
        echo "nerdctl command not found. Please install nerdctl first."
        exit 1
    fi
}

do_install_helm()
{
    # Check if helm exists
    if ! command -v helm &> /dev/null
    then
        echo "helm command not found, starting download..."

        # Set architecture variable
        arch=""
        case "$(uname -m)" in
            arm64|aarch64)
                arch="arm64"
                ;;
            x86_64|amd64)
                arch="amd64"
                ;;
            *)
                echo "Unsupported architecture: $(uname -m)"
                exit 1
                ;;
        esac

        # Set helm version
        helm_version="3.16.4"

        # Download and extract helm in a single command
        echo "Downloading and extracting helm v${helm_version} (linux-${arch})..."
        curl -sSL "https://get.helm.sh/helm-v${helm_version}-linux-${arch}.tar.gz" | tar -xz -C /tmp/ linux-${arch}/helm

        # Move to /usr/local/bin
        echo "Installing helm to /usr/local/bin/..."
        sudo mv /tmp/linux-${arch}/helm /usr/local/bin/helm

        # Clean up temp directory (optional, as /tmp usually gets cleared on reboot)
        # rm -rf /tmp/linux-${arch}

        echo "helm v${helm_version} has been successfully installed."
    else
        echo "helm already exists."
    fi
}


do_install_nerdctl

do_install_docker_wrapper

do_install_helm
